package com.xjscrm.console.web.user;

import com.google.common.collect.ImmutableMap;
import com.xjscrm.common.entity.Merchant;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.common.util.LoginContext;
import com.xjscrm.console.param.BaseDto;
import com.xjscrm.console.param.user.*;
import com.xjscrm.console.service.common.user.UserService;
import com.xjscrm.console.service.customer.childUser.ChildMerchatService;
import com.xjscrm.console.vo.ChildMerchantBaseInfoVo;
import com.xjscrm.console.vo.MerchantBaseInfoVo;
import com.xjscrm.console.vo.MerchatChildVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Api(value = "用户管理", tags = "用户管理")
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private ChildMerchatService childMerchatService;

    @Autowired
    private UserService userService;


    @ApiOperation(value = "新增员工", notes = "新增员工",response = Boolean.class)
    @PostMapping(value ="/addChildMerchat" )
    public Object addChildMerchat(@RequestBody ChildMerchatDto childMerchatDto)throws Exception{
         userService.addMerchatChildmerchatRelation(childMerchatDto);
         childMerchatService.addChildMerchat(childMerchatDto);
         return true;
    }
    @ApiOperation(value = "修改员工", notes = "修改员工",response = Boolean.class)
    @PostMapping(value ="/updateChildMerchat" )
    public Object updateChildMerchat(@RequestBody ChildMerchatDto childMerchatDto)throws Exception{
        HashMap<String, Object> map = childMerchatService.updateChildMerchat(childMerchatDto);
        if("1".equals(map.get("ifUpdateAccount").toString())){
            userService.updateMerchatChildmerchatRelation(map);
        }
        return true;
    }

    @ApiOperation(value = "获取主账号基本信息", notes = "获取主账号基本信息",response = MerchantBaseInfoVo.class)
    @PostMapping(value ="/getMerchatBaseInfo" )
    public Object getMerchantBaseInfo(@RequestBody BaseDto baseDto)throws Exception{
       return userService.getMerchantBaseInfo(baseDto);
    }

    @ApiOperation(value = "获取子账号基本信息", notes = "获取子账号基本信息",response = ChildMerchantBaseInfoVo.class)
    @PostMapping(value ="/getChildMerchantBaseInfo" )
    public Object getChildMerchantBaseInfo(@RequestBody BaseDto baseDto)throws Exception{
        return childMerchatService.getChildMerchantBaseInfo(baseDto);
    }

    @ApiOperation(value = "修改主账号头像", notes = "修改主账号头像",response = Boolean.class)
    @PostMapping(value ="/changeMerchantHeadImage" )
    public Object changeMerchantHeadImage(@RequestBody ChangeHeadImageDto changeMHeadImageDto)throws Exception{
         userService.changeMerchantHeadImage(changeMHeadImageDto);
         return true;
    }


    @ApiOperation(value = "修改子账号头像", notes = "修改子账号头像",response = Boolean.class)
    @PostMapping(value ="/changechildMerchantHeadImage" )
    public Object changechildMerchantHeadImage(@RequestBody ChangeHeadImageDto changeHeadImageDto)throws Exception{
        childMerchatService.changechildMerchantHeadImage(changeHeadImageDto);
        return true;
    }

    @ApiOperation(value = "新增员工分组", notes = "新增员工分组",response = Boolean.class)
    @PostMapping(value ="/addStaffGroup" )
    public Object addStaffGroup(@RequestBody StaffGroupDto staffGroupDto)throws Exception{
        childMerchatService.addStaffGroup(staffGroupDto);
        return true;
    }

    @ApiOperation(value = "获取员工分组列表", notes = "获取员工分组列表",response = Map.class)
    @PostMapping(value ="/getStaffGroupList" )
    public Object getStaffGroupList(@RequestBody BaseDto baseDto)throws Exception{
        return childMerchatService.getStaffGroupList(baseDto);
    }

    @ApiOperation(value = "修改员工分组", notes = "修改员工分组",response = Boolean.class)
    @PostMapping(value ="/updateStaffGroup" )
    public Object updateStaffGroup(@RequestBody StaffGroupDto staffGroupDto)throws Exception{
        childMerchatService.updateStaffGroup(staffGroupDto);
        return true;
    }

    @ApiOperation(value = "删除员工分组", notes = "删除员工分组",response = Boolean.class)
    @PostMapping(value ="/deleStaffGroup" )
    public Object deleStaffGroup(@RequestBody StaffGroupDto staffGroupDto)throws Exception{
        childMerchatService.deleStaffGroup(staffGroupDto);
        return true;
    }


    @ApiOperation(value = "获取员工列表", notes = "获取员工列表",response = MerchatChildVo.class)
    @PostMapping(value ="/getStaffList" )
    public Object getStaffList(@RequestBody ChildMerchatQuery childMerchatQuery)throws Exception{
        return childMerchatService.getStaffList(childMerchatQuery);
    }

    @ApiOperation(value = "换分组", notes = "换分组",response = Boolean.class)
    @PostMapping(value ="/chageGroup" )
    public Object chageGroup(@RequestBody ChageGroupDto chageGroupDto)throws Exception{
        childMerchatService.chageGroup(chageGroupDto);
        return true;
    }

    @ApiOperation(value = "批量换分组", notes = "批量换分组",response = Boolean.class)
    @PostMapping(value ="/batchchageGroup" )
    public Object batchchageGroup(@RequestBody ChageGroupDto chageGroupDto)throws Exception{
        childMerchatService.batchchageGroup(chageGroupDto);
        return true;
    }

    @ApiOperation(value = "获取分组下拉", notes = "获取分组下拉",response = Map.class)
    @PostMapping(value ="/getStaffGroupDropDwon" )
    public Object getStaffGroupDropDwon(@RequestBody BaseDto baseDto)throws Exception{
       return childMerchatService.getStaffGroupDropDwon(baseDto);

    }


    @ApiOperation(value = "批量删除员工", notes = "获取分组下拉",response = Boolean.class)
    @PostMapping(value ="/batchDeleStaff" )
    public Object batchDeleStaff(@RequestBody BatchDeleStaffDto batchDeleStaffDto)throws Exception{
        List<String> userAccounts = childMerchatService.batchDeleStaff(batchDeleStaffDto);
        //查看是否删除成功，成功后删除子账号和主账号的关系
        if(userAccounts!=null&&!userAccounts.isEmpty()){
            DeleMerchatChildmerchatRelationDto deleMerchatChildmerchatRelationDto = new DeleMerchatChildmerchatRelationDto();
            deleMerchatChildmerchatRelationDto.setMerId(batchDeleStaffDto.getMerId());
            deleMerchatChildmerchatRelationDto.setUserAccounts(userAccounts);
            userService.BatchDeleMerchatChildmerchatRelation(deleMerchatChildmerchatRelationDto);
        }
        return true;
    }

    @ApiOperation(value = "批量修改员工会话数量限制", notes = "批量修改员工会话数量限制", response = Boolean.class)
    @PostMapping(value = "/batch/editChatLimitCount")
    public Object batchEditChatLimitCount(@RequestBody StaffBatchOptDto staffBatchOptDto) throws Exception {
        if (Objects.isNull(staffBatchOptDto) || ObjectUtils.isEmpty(staffBatchOptDto.getIds()) || Objects.isNull(staffBatchOptDto.getChatLimitCount())) {
            throw new BusinessException("参数错误");
        }

        childMerchatService.batchEditChatLimitCount(LoginContext.getLogin(), staffBatchOptDto);
        return true;
    }

    @ApiOperation(value = "获取商户app密码", notes = "获取商户app密码", response = String.class)
    @PostMapping(value = "/getAppPwd")
    public Object getAppPwd(@RequestBody AppPwdDto appPwdDto)throws Exception{
        Merchant merchant = userService.getAppPwd(appPwdDto);
        Object pppPwd = childMerchatService.getPppPwd(merchant.getId());
        return ImmutableMap.of("merId", merchant.getId(), "pwd", pppPwd);
    }
}
